package com.gzhryc.shared_device.oem.code.device;

import com.gzhryc.common.exceptions.LogicException;
import com.gzhryc.common.logging.Logger;
import com.gzhryc.jdbc.JdbcSession;
import com.gzhryc.shared_device.bxscn.code.BXSCNProduceConfig;
import com.gzhryc.shared_device.bxscn.code.services.PortableEnergyCabinetService;
import com.gzhryc.shared_device.cdb.code.CDBProduceConfig;
import com.gzhryc.shared_device.cdb.code.services.BatteryCabinetService;
import com.gzhryc.shared_device.cdcz.code.CDCZProduceConfig;
import com.gzhryc.shared_device.cdcz.code.services.ChargeSocketService;
import com.gzhryc.shared_device.common.device.dao.db.DeviceProduceBatch;
import com.gzhryc.shared_device.common.device.dao.db.DeviceTypeFunc;
import com.gzhryc.shared_device.common.device.dao.enums.DeviceProduceBatchEnum;
import com.gzhryc.shared_device.common.device.services.DeviceProduceBatchService;
import com.gzhryc.shared_device.pcl.code.PCLProduceConfig;
import com.gzhryc.shared_device.pcl.code.services.ChargingCableService;

import java.sql.SQLException;
import java.util.List;
import java.util.Map;

public class DeviceProduceBatchManager {

    static Logger log = Logger.getLogger(DeviceProduceBatchManager.class);

    static DeviceProduceBatchManager me;

    public static DeviceProduceBatchManager self(){
        if(me == null){
            me = new DeviceProduceBatchManager();
        }
        return me;
    }

    public boolean createFuncDevice(DeviceProduceBatch produceBatch, List<DeviceTypeFunc> deviceTypeFuncList, Map<String,Object> configContent) throws LogicException {
        JdbcSession.begin();
        try {
            if (DeviceProduceBatchService.self().producingToTesting(produceBatch.getBatchId(),configContent)) {
                if (deviceTypeFuncList != null && deviceTypeFuncList.size() > 0) {
                    boolean flag = true;
                    for (DeviceTypeFunc func : deviceTypeFuncList) {
                        if (EDeviceFunCode.CDCZ.index().equals(func.getCode())) {
                            Object obj = configContent.get(func.getCode());
                            if(obj != null) {
                                flag = ChargeSocketService.self().produceDevice(produceBatch, func, CDCZProduceConfig.class.cast(obj));
                            }else{
                                throw new LogicException("充电插座参数未配置");
                            }
                        }else if(EDeviceFunCode.CDB.index().equals(func.getCode())){
                            Object obj = configContent.get(func.getCode());
                            if(obj != null) {
                                flag = BatteryCabinetService.self().produceDevice(produceBatch, func, CDBProduceConfig.class.cast(obj));
                            }else{
                                throw new LogicException("共享充电宝参数未配置");
                            }
                        }else if(EDeviceFunCode.BXSCN.index().equals(func.getCode())){
                            Object obj = configContent.get(func.getCode());
                            if(obj != null) {
                                flag = PortableEnergyCabinetService.self().produceDevice(produceBatch, func, BXSCNProduceConfig.class.cast(obj));
                            }else{
                                throw new LogicException("便携式电池参数未配置");
                            }
                        }else if(EDeviceFunCode.PCL.index().equals(func.getCode())){
                            Object obj = configContent.get(func.getCode());
                            if(obj != null) {
                                flag = ChargingCableService.self().produceDevice(produceBatch, func, PCLProduceConfig.class.cast(obj));
                            }else{
                                throw new LogicException("便携式电池参数未配置");
                            }
                        }else{
                            DeviceProduceBatchService.self().updateState(produceBatch.getBatchId(), DeviceProduceBatchEnum.State.Producing.index());
                            throw new LogicException("不支持的设备功能");
                        }

                        if(!flag){
                            break;
                        }
                    }

                    if(flag){
                        JdbcSession.commit();
                        return true;
                    }
                }
            }
        } catch (SQLException e) {

        }finally {
            JdbcSession.end();
        }
        return false;
    }
}
